﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DiemChapNhanThe.Entities;
using DiemChapNhanThe.Data;
using System.Data;


public partial class Admin_DanhMuc_DMSubMatHang : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindCatalog.BindLoaiMatHang(cboLoaiMatHang);
            Pager1.CurrentIndex = 1;
            Bind_Data(Pager1.CurrentIndex);
        }
    }
    protected void Bind_Data(int PageNo)
    {
        int TotalRows = 0;
        string WhereClause = "1=1";
        DataSet dsBookCategories = DataRepository.Provider.ExecuteDataSet("SubMatHang_GetPaged", WhereClause, "Id DESC", PageNo - 1, Pager1.PageSize);
        gridCatalog.DataSource = dsBookCategories.Tables[0];
        gridCatalog.DataBind();
        try
        {
            TotalRows = Convert.ToInt32(dsBookCategories.Tables[1].Rows[0][0]);
        }
        catch (Exception)
        {
        }
        Pager1.ItemCount = TotalRows;
        if (TotalRows <= Pager1.PageSize)
            Pager1.Visible = false;
        else
            Pager1.Visible = true;

    }

    protected void Pager1_Command(object sender, CommandEventArgs e)
    {
        try
        {
            int currnetPageIndex = Convert.ToInt32(e.CommandArgument);
            Pager1.CurrentIndex = currnetPageIndex;
            Bind_Data(currnetPageIndex);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMSubMatHang.aspx.cs", "Pager1_Command()", ex.ToString());
        }
    }

    protected void cmdSave_Click(object sender, ImageClickEventArgs e)
    {
        bool updateStatus = false;
        try
        {
            SubMatHang entSubMatHang = new SubMatHang();
            entSubMatHang.Id = Convert.ToInt32(H_CatalogId.Value);
            entSubMatHang.TenSubMatHang = txtTen.Text;
            entSubMatHang.IdLoaiMatHang = Convert.ToInt32(cboLoaiMatHang.SelectedValue);
            entSubMatHang.TrangThai = chkTrangThai.Checked;

            if (entSubMatHang.Id == 0)
            {
                entSubMatHang.Id = 0;
                updateStatus = DataRepository.SubMatHangProvider.Insert(entSubMatHang);
            }
            else
            {
                updateStatus = DataRepository.SubMatHangProvider.Update(entSubMatHang);
            }
            if (updateStatus)
            {
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.SUBMATHANG_CAPNHAT_THANHCONG, true);
                Pager1.CurrentIndex = 1;
                Bind_Data(Pager1.CurrentIndex);
                cmdReset_Click(sender, e);
            }
            else
                ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.SUBMATHANG_CAPNHAT_THATBAI, true);
        }
        catch (Exception ex)
        {
            Common.WriteLog("DMSubMatHang.aspx.cs", "cmdSave_Click", ex.ToString());
            ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), Constants.THATBAI, Constants.SUBMATHANG_CAPNHAT_THATBAI, true);
        }
    }

    protected void cmdReset_Click(object sender, ImageClickEventArgs e)
    {
        txtTen.Text = "";
        cboLoaiMatHang.SelectedValue = "0";
        H_CatalogId.Value = "0";
    }

    protected void gridCatalog_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.backgroundColor='WhiteSmoke';";
                e.Row.Attributes["onmouseout"] = "this.style.backgroundColor='White';";

                HiddenField H_IdCatalog = (HiddenField)e.Row.FindControl("H_IdCatalog");
                e.Row.Attributes.Add("ondblclick", "Javascript:DoubleClick_SubMatHang('" + H_IdCatalog.Value + "');");
            }
        }
        catch (Exception ex)
        {
            Common.WriteLog("Admin/DanhMuc/DMSubMatHang.aspx.cs", "gridCatalog_RowDataBound", ex.ToString());
        }
    }

    protected void gridCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Select")
        {
            try
            {
                H_CatalogId.Value = e.CommandArgument.ToString();
                SubMatHang entSubMatHang = DataRepository.SubMatHangProvider.GetById(Convert.ToInt32(H_CatalogId.Value));
                if (entSubMatHang != null)
                {
                    txtTen.Text = entSubMatHang.TenSubMatHang;
                    cboLoaiMatHang.SelectedValue = entSubMatHang.IdLoaiMatHang.ToString();
                }
            }
            catch (Exception ex)
            {
                Common.WriteLog("DMSubMatHang.aspx.cs", "gridCatalog_RowCommand - Select", ex.ToString());
            }
        }
    }

    public string GetLoaiMatHang(object Char)
    {
        int idMatHang = Convert.ToInt32(Char.ToString());
        using (LoaiMatHang entMatHang = DataRepository.LoaiMatHangProvider.GetById(idMatHang))
        {
            return entMatHang.TenMatHang;
        }
    }
    protected void cmdXoa_Click(object sender, ImageClickEventArgs e)
    {
        for (int i = 0; i < gridCatalog.Rows.Count; i++)
        {
            CheckBox chkSelect = (CheckBox)gridCatalog.Rows[i].Cells[0].FindControl("chkSelect");
            if (chkSelect.Checked)
            {
                HiddenField H_IdCatalog = (HiddenField)gridCatalog.Rows[i].Cells[0].FindControl("H_IdCatalog");
                bool deleteStatus = DataRepository.SubMatHangProvider.Delete(Convert.ToInt32(H_IdCatalog.Value));
            }
        }
        Bind_Data(Pager1.CurrentIndex);
    }
}